<script type="text/javascript">
function ChangeType()
{
        var type_id = $("#type").val(); 
        var url = '<?php echo osc_ajax_plugin_url("cars_attributes/ajax.php") . "&typeId="; ?>' + type_id;
        var result = ''; 						

        if ($("#type>option:selected").val() != "")
                $("#cat").val($("#type>option:selected").text());
        else 
                $("#cat").val("");

        if(type_id != '') {
            $("#make").attr('disabled',false);

            $.ajax({
                type: "POST",
                url: url,
                dataType: 'json',
                success: function(data){
                    var length = data.length;
                    if(length > 0) {
                        result += '<option value=""><?php _e("Select a make", 'cars_attributes'); ?></option>';
                        for(key in data) {
                            result += '<option value="' + data[key].pk_i_id + '">' + data[key].s_name + '</option>';
                        }
                          result += '<option value="-1"><?php _e("Others", 'cars_attributes'); ?></option>';
                          $("#makePnl").css("display","");
                          //$("#modelPnl").css("display","");
                    } else {
                        result += '<option value=""><?php _e('No results', 'cars_attributes'); ?></option>';
                         $("#model").html('<option value=""><?php _e("Select a model", 'cars_attributes'); ?></option>');
                         $("#model").attr('disabled',true);
                         $("#makePnl").css("display","none");
                         $("#modelPnl").css("display","none");
                    }
                    $("#make").html(result);
                }
             });
         } else {
            $("#make").attr('disabled',true);
            $("#model").html('<option value=""><?php _e("Select a model", 'cars_attributes'); ?></option>');
            $("#model").attr('disabled',true);
            $("#makePnl").css("display","none");
            $("#modelPnl").css("display","none");
         }
}
 
 function preLoadSearchAttr()
 {
    var idVehicleType = $("#type").val();
    $("#axisPnl").css("display", "");
     
    switch(idVehicleType) {
     case "7": //maquinaria                
     case "8": //Autobus
     case "9": //Furgoneta       
         $("#axisPnl").css("display", "none");
         break;     
    }  
 }

$(document).ready(function(){

    $("#type").change(function(){
      ChangeType();
    });
		
    $("#make").change(function(){
      var make_id = $(this).val();
        var url = '<?php echo osc_ajax_plugin_url("cars_attributes/ajax.php") . "&makeId="; ?>' + make_id;
        var result = '';
        if(make_id != '') {

            $("#model").attr('disabled',false);
            $.ajax({
                type: "POST",
                url: url,
                dataType: 'json',
                success: function(data){
                    var length = data.length;
                    if(length > 0) {
                        result += '<option value=""><?php _e("Select a model", 'cars_attributes'); ?></option>';
                        for(key in data) {
                            result += '<option value="' + data[key].pk_i_id + '">' + data[key].s_name + '</option>';
                        }
                         result += '<option value="-1"><?php _e("Others", 'cars_attributes'); ?></option>';
                         $("#modelPnl").css("display","");
                    } else {
                        result += '<option value=""><?php _e('No results', 'cars_attributes'); ?></option>';
                        $("#modelPnl").css("display","none");
                    }
                    $("#model").html(result);
                }
             });
         } else {
            $("#model").attr('disabled',true);
         }
    });

    //Select default Type
    if ($("#type>option:selected").text() != '' && $("#make").val() == '')
    {					
        ChangeType();
    }
    
    //Load attr
    preLoadSearchAttr();
});
</script>
<?php 
    $make  = Params::getParam('make') ;
    $model = Params::getParam('model') ;
    $type  = Params::getParam('type') ;	
    $cat = Params::getParam('sCategory');
    $year =  Params::getParam('year') ;	
    $km =  Params::getParam('km') ;	
    $axis =  Params::getParam('axis') ;	
	
    //$makes  = ModelCars::newInstance()->getCarMakes();
    $makes = array();
	if($type != '') {
        $makes = ModelCars::newInstance()->getCarMakesByType($type);
    }
	$models = array();
    if($make != '') {
        $models = ModelCars::newInstance()->getCarModels($make);
    }

    $types = ModelCars::newInstance()->getVehiclesType(osc_current_user_locale());
	
	
?>

<fieldset>
   <fieldset id="typePnl">
        <h5><?php _e('Car type', 'cars_attributes'); ?></h5>
        <select name="type" id="type">
            <option value=""><?php _e('Select a car type', 'cars_attributes'); ?></option>
            <?php foreach($types as $p) { ?>
                <option value="<?php echo $p['pk_i_id']; ?>" <?php if($type==$p['pk_i_id'] || ($type == '' && $cat != '' && strcasecmp($cat,sanear_string($p['s_name']))==0)) { echo 'selected'; } ?>><?php echo $p['s_name']; ?></option>
            <?php } ?>
        </select>
    </fieldset>
    <fieldset id="makePnl">
        <h5><?php _e('Make', 'cars_attributes'); ?></h5>
        <select name="make" id="make" >
            <option value=""><?php  _e('Select a make', 'cars_attributes'); ?></option>
            <?php foreach($makes as $m) { ?>
                <option value="<?php echo $m['pk_i_id']; ?>" <?php if($make == $m['pk_i_id']) { echo 'selected'; } ?>><?php echo $m['s_name']; ?></option>
            <?php } 
            if( count($makes)>0) { ?>
            <option value="-1" <?php if($make == -1) { echo 'selected'; } ?>><?php _e('Others', 'cars_attributes'); ?></option>    
            <?php } ?>
        </select>
    </fieldset>
    <fieldset id="modelPnl" <?php if(count($models)<=0) { echo "style='display:none;'"; } ?> >
        <h5><?php _e('Model', 'cars_attributes'); ?></h5>
        <select name="model" id="model">
            <option value=""><?php _e('Select a model', 'cars_attributes'); ?></option>
            <?php foreach($models as $m) { ?>
                <option value="<?php echo $m['pk_i_id']; ?>" <?php if($model == $m['pk_i_id']) { echo 'selected';} ?>><?php echo $m['s_name']; ?></option>
            <?php }
            if( count($models)>0) { ?>
             <option value="-1" <?php if($model == -1) { echo 'selected'; } ?>><?php _e('Others', 'cars_attributes'); ?></option>    
             <?php } ?>
        </select>
    </fieldset>
	<hr/>
	
	<fieldset>           
		<div style="display:inline-block;width:100%;margin-top:10px;">
			<div style="float:left;">
               <span style="font-size: 14px;font-weight: bold;"><?php _e('km', 'cars_attributes') ; ?></span>
               <input type="text" id="km" name="km" value="<?php if($km != '') echo $km; ?>" size="4" maxlength="6" />	
			</div>
			<div style="float:right;">
				<span style="font-size: 14px;font-weight: bold;"><?php _e('year', 'cars_attributes') ; ?></span>
				<input type="text" id="year" name="year" value="<?php if ($year != '') echo $year; ?>" size="4" maxlength="4" />
			</div>
		</div>
		<div style="display:inline-block;width:100%;text-transform:lowercase;">
			<span style="float:left;font-size:8pt;">(<?php _e('from', 'cars_attributes') ; ?>)</span>	
			<span style="float:right;margin-right:70px;font-size:8pt;">(<?php _e('from', 'cars_attributes') ; ?>)</span>	
		</div>
	</fieldset>
	
	
	
	<fieldset id="axisPnl"><hr/>
        <h5><?php _e('Axis', 'cars_attributes'); ?></h5>
                <select name="axis" id="axis">
                        <option value=""><?php _e('Select Axis', 'cars_attributes'); ?></option>
                        <option value="4x2" <?php if($axis == '4x2') { echo 'selected'; } ?>>4x2</option>
                        <option value="4x4" <?php if($axis == '4x4') { echo 'selected'; } ?>>4x4</option>
                        <option value="6x2" <?php if($axis == '6x2') { echo 'selected'; } ?>>6x2</option>
                        <option value="6x4" <?php if($axis == '6x4') { echo 'selected'; } ?>>6x4</option>
                        <option value="8x4" <?php if($axis == '8x4') { echo 'selected'; } ?>>8x4</option>
                        <option value="8x8" <?php if($axis == '8x8') { echo 'selected'; } ?>>8x8</option>
                </select>     
        </fieldset>
	
	
    <!--<div class="row one_input" style="display:none;">
        <?php //$transmission = Params::getParam('transmission') ; ?>
        <h6 for="transmission"><?php //_e('Transmission', 'cars_attributes'); ?></h6>
        <input style="width:20px;" type="radio" name="transmission" value="MANUAL" id="manual" <?php //if($transmission == 'MANUAL') { echo 'checked="yes"'; } ?>/> <label for="manual"><?php //_e('Manual', 'cars_attributes'); ?></label><br />
        <input style="width:20px;" type="radio" name="transmission" value="AUTO" id="auto" <?php //if($transmission == 'AUTO') { echo 'checked="yes"'; } ?>/> <label for="auto"><?php //_e('Automatic', 'cars_attributes'); ?></label>
    </div>-->
</fieldset>